home *** CD-ROM | disk | FTP | other *** search
/ The Complete Utilities To…ka 501 Killer Utilities! / 501 Killer Utilities! (Macworld July 1995).cdr / Programming / Tools Plus 2.5.3 Evaluation Kit / Tools Plus 2.5.3 / Demos / Demo for THINK C⁄C++ / PascalStrHandles.c < prev   
Encoding:
Text File  |  1994-12-12  |  2.4 KB  |  56 lines  |  [TEXT/KAHL]

  1. /*     Tools Plus 2.5.3 -- "C / Pascal String Handles" supplemental file
  2.  *    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3.  *
  4.  *    re: Editing Fields
  5.  *
  6.  *    Each Tools Plus editing field uses a “string handle” (ie: a handle to a Pascal
  7.  *    string) to store the text that is displayed in the field.  The following
  8.  *    routines have been provided to facilitate moving C strings into Pascal Handle
  9.  *    structures, and vice versa.
  10.  *
  11.  *    A Pascal string’s first byte (byte-0) is a length byte, and the string is not
  12.  *    null-terminated. Pascal strings are limited to 255 characters.
  13.  *
  14.  *    NOTE: These routines do not check to determine if the destination variable
  15.  *                is large enough to accommodate the text being copied there.  You may
  16.  *                want to modify these routines to do such validation.
  17.  */
  18.  
  19.  
  20.  
  21. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  22.  *    Copy a C string into a Pascal “string handle”
  23.  */
  24. void Cstr2PHdl(char CSource[], Handle PDest);    // (Prototype)
  25. void Cstr2PHdl(char CSource[],                                /*Source C string                                        */
  26.                              Handle PDest)                                    /*Handle to destination Pascal            */
  27.                                                                                             /*    string.                                                    */
  28.     {
  29.     short            length;                                                        /*Source string's length                        */
  30.  
  31.  
  32.     length = strlen(CSource);                                        /*Determine length of source string    */
  33.     (**PDest) = (unsigned char)length;                    /*Copy length-byte into byte-0            */
  34.     BlockMove(CSource, (*PDest) + 1, length);        /*Copy source C string (omitting        */
  35.                                                                                             /*    null terminator) to the Pascal    */
  36.                                                                                             /*    string right after the length-    */
  37.     }                                                                                        /*    byte.                                                        */
  38.  
  39.  
  40.  
  41.  
  42. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  43.  *    Copy the contents of a Pascal “string handle” into a C string
  44.  */
  45. void PHdl2Cstr(Handle PSource, char CDest[]);    //    (Prototype)
  46. void PHdl2Cstr(Handle PSource,                                /*Handle to source Pascal string        */
  47.                              char CDest[])                                    /*Destination C string                            */
  48.     {
  49.     short            length;                                                        /*Source string's length                        */
  50.  
  51.     length = (unsigned char)(**PSource);                /*Determine length of source string    */
  52.     BlockMove((*PSource) + 1, CDest, length);        /*Copy source Pascal string                    */
  53.                                                                                             /*    (omitting the length byte) to        */
  54.                                                                                             /*    the C string.                                        */
  55.     CDest[length] = '\0';                                                /*Null-terminate the C string                */
  56.     }                                                                                        /*                                                                    */